package com.cqnu.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cqnu.common.Constant;
import com.cqnu.entity.Student;
import com.cqnu.entity.SuperUser;
import com.cqnu.entity.Teacher;
import com.cqnu.util.CommonUtil;
import com.cqnu.util.MD5Util;
import com.cqnu.util.StringUtil;


/**
 * 
 */
@Controller
@RequestMapping(value = "/login.do")
public class LoginController extends BaseController {


	@RequestMapping(method = RequestMethod.POST)
	public String login(@RequestParam("user") String user, @RequestParam("pass") String pass, @RequestParam("type") String type, HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		if (StringUtil.isBlank(user) || StringUtil.isBlank(pass) || StringUtil.isBlank(type)) {
			modelMap.addAttribute(Constant.MSG, "用户名和密码不能为空！");
			return "login";
		}

		//设置账号和密码
		int number = Integer.parseInt(user);
		String MD5pass = MD5Util.MD5(pass);
		
		//判断type，根据type登录（0:超级用户；1：老师；2：学生）
		if( Constant.LOGIN_TYPE_STU.equals(type) ){
			Student loginUser = new Student();
			loginUser.setNumber(number);
			loginUser.setPassword(MD5pass);
			
			Student dto = studentService.login(loginUser);
			
			if (dto == null) {
				modelMap.addAttribute(Constant.MSG, "用户名不存在！");
				return "login";
			}
			
			//设置Session，用户信息以及角色信息
			CommonUtil.addCookie(response, "uid", user, Integer.MAX_VALUE);
			this.setSession(request, Constant.SESSION_USER_KEY, dto);
			this.setSession(request, Constant.SESSION_ROLE_KEY, Constant.LOGIN_TYPE_STU);
			
			return "redirect:/home/goStuMainPage.do";
		}
		//判断type，根据type登录（0:超级用户；1：老师；2：学生）
		else if( Constant.LOGIN_TYPE_TEA.equals(type) ){
			Teacher loginUser = new Teacher();
			loginUser.setNumber(number);
			loginUser.setPassword(MD5pass);
			
			Teacher dto = teacherService.login(loginUser);
			
			if (dto == null) {
				modelMap.addAttribute(Constant.MSG, "用户名不存在！");
				return "login";
			}
			
			//设置Session，用户信息以及角色信息
			CommonUtil.addCookie(response, "uid", user, Integer.MAX_VALUE);
			this.setSession(request, Constant.SESSION_USER_KEY, dto);
			this.setSession(request, Constant.SESSION_ROLE_KEY, Constant.LOGIN_TYPE_TEA);
			
			return "redirect:/home/goTeaMainPage.do";
		}
		//判断type，根据type登录（0:超级用户；1：老师；2：学生）
		else if( Constant.LOGIN_TYPE_ADMIN.equals(type) ){
			SuperUser loginUser = new SuperUser();
			loginUser.setNumber(number);
			loginUser.setPassword(pass);
			
			SuperUser dto = superUserService.login(loginUser);
			
			if (dto == null) {
				modelMap.addAttribute(Constant.MSG, "用户名不存在！");
				return "login";
			}
			
			//设置Session，用户信息以及角色信息
			CommonUtil.addCookie(response, "uid", user, Integer.MAX_VALUE);
			this.setSession(request, Constant.SESSION_USER_KEY, dto);
			this.setSession(request, Constant.SESSION_ROLE_KEY, Constant.LOGIN_TYPE_ADMIN);
			
			return "redirect:/home/goSuperMainPage.do";
		}

		modelMap.addAttribute(Constant.MSG, "登录失败！");
		return "login";
	}

	@RequestMapping(method = RequestMethod.GET)
	public String logout(HttpServletRequest request, HttpSession session) {
		this.removeSession(request, Constant.SESSION_USER_KEY);
		if (session != null) {
			session.invalidate();
		}
		return "redirect:/";
	}

}
